package edu.ccut.saturn.workflow.startWorkFlowModule.code;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.operation.Operation;
import edu.ccut.saturn.workflow.common.Constants;
import edu.ccut.saturn.workflow.common.WorkFlowTools;
import edu.ccut.saturn.workflow.model.ActivityInfo;
import edu.ccut.saturn.workflow.model.UserInfo;

public class GetParticipantFromTree implements IParticipantGeter {

	public List<SaturnData> getParticipant(SaturnData workItemInstance,
			ActivityInfo currentActivityInfo, IParameterContext parameterContext)
			throws Exception {
		String userSerialNo = null;
		String userName = null;
		HashMap<String, String> workFlowParamMap = (HashMap<String, String>) parameterContext
		.getParameterValue(2);
		String bussinessType = workFlowParamMap.get("bussiness_type");//TODO bussiness_type
		Connection conn = parameterContext.getConnection();
		List<SaturnData> wiParticipantDatas = new ArrayList<SaturnData>();
		List<UserInfo> userInfos = currentActivityInfo.getUsers();
		for (int i = 0; i < userInfos.size(); i++) {
			UserInfo userInfo = userInfos.get(i);
			if (userInfo.getType().equals(Constants.PARTICIPANT_TYPE_USER)) {//指定user不需要过滤
				wiParticipantDatas.add(
						WorkFlowTools.getParticipantInstanceWithValues(userInfo.getId(),userInfo
						.getName()));
			} else {//参与者为集合描述
				List<SaturnData> userDataList = WorkFlowTools.getUserDataListByTypeAndId(userInfo.getType(), userInfo.getId(), conn);
				
				if(userDataList!=null){
					for (int j = 0; j < userDataList.size(); j++) {
						SaturnData userData = userDataList.get(j);
						// TODO 得到的userData 按照定制的规则筛选出真正可以执行的user插入参与者表,例如前驱活动的user要和欲处理用户在同一部门
						userSerialNo =userData.getString("id");
						userName = userData.getString("username");
						String validateuserlogic_fullkey = currentActivityInfo.getValidateUserLogic();
						
						if (validateuserlogic_fullkey != null && !"".equals(validateuserlogic_fullkey)) {
							boolean isValidUser = WorkFlowTools.executeParticipantFilterLogic(validateuserlogic_fullkey,
									workItemInstance.get("previous_assistant")+"",userData.get("id")+"",bussinessType);//TODO bussiness_type
							if (isValidUser) {
								SaturnData wiParticipantData = WorkFlowTools.getParticipantInstanceWithValues( userSerialNo, userName);
								wiParticipantDatas.add(wiParticipantData);
							}
						} else {//不用交验
							SaturnData wiParticipantData = WorkFlowTools.getParticipantInstanceWithValues(userSerialNo, userName);
							wiParticipantDatas.add(wiParticipantData);
						}
					}
				}else{
					throw new  SaturnComponentException("\u4e0b\u4e00\u6d3b\u52a8\u65e0\u6d3b\u52a8\u53c2\u4e0e\u8005\uff01");
				}
			}
		}
		
		return wiParticipantDatas;
	}



}
